from django.db import models


class Books(models.Model):
    name = models.CharField(max_length=64)
    price = models.DecimalField(max_digits=5, decimal_places=2)

    publish = models.ForeignKey(to='Publish', on_delete=models.CASCADE)
    authors = models.ManyToManyField(to='Author')

    def __str__(self):
        return self.name

    def publish_detail(self):
        return {
            'name': self.publish.name,
            'addr': self.publish.addr
        }

    def author_list(self):
        l = []
        for i in self.authors.all():
            l.append({
                'name': i.name,
                'age': i.age
            })
        return l


class Publish(models.Model):
    name = models.CharField(max_length=64)
    addr = models.CharField(max_length=64)

    def __str__(self):
        return self.name


class Author(models.Model):
    name = models.CharField(max_length=64)
    age = models.IntegerField()

    def __str__(self):
        return self.name
